<?php

if(!is_null(App::$Current->User) && !is_null(App::$Current->Page->User))
{
	if(post('form') == 'rating' )
	{
		updateRating();
	}
	else
	{
		$Obj = new TemplateLoader( );
		$Obj->Template = 'templates/rating.tpl';
		$Obj->Module = 'SocioRating';
		$Obj->Rated = App::$Current->Page->User;
		$AccInfo = UserManager::GetUser( App::$Current->Page->User);
		$Obj->RatedName = $AccInfo->fullname;
		$Obj->Rating = RatingManager::GetRating(App::$Current->User, App::$Current->Page->User);
		//App::$Current->Page->Content[] = $Obj->Render();
		$Obj->Render();
	}
}

function updateRating()
{
	$Creative = $_POST['creative'];
	$Helpful = $_POST['helpful'];
	$Accurate = $_POST['accurate'];
	$Gooder = $_POST['gooder'];
	$Critical = $_POST['critical'];
	$Friendly = $_POST['friendly'];
	$Trustworthy = $_POST['trustworthy'];
	$Comment = $_POST['comment'];
	
	$Result = RatingManager::UpdateRating(App::$Current->User, App::$Current->Page->User, $Accurate, $Creative, $Critical, $Friendly, $Gooder, $Helpful, $Trustworthy, $Comment);
	$RatedAcc = UserManager::GetUser( App::$Current->Page->User );
	
	if( $Result == false )
	{
		$fail = a2o('TemplateLoader', array( 'Module' => 'SocioRating', 'Template' => 'templates/ratingfail.tpl', 'Name' => $RatedAcc['FullName']));
		$fail->Render( );
	}
	else
	{
		$success = a2o('TemplateLoader', array( 'Module' => 'SocioRating', 'Template' => 'templates/ratingsuccess.tpl', 'Name' => $RatedAcc['FullName']));
		$success->Render( );
		
		// send the person that is rated an email
		$RaterAcc = UserManager::GetUser( App::$Current->User );
		
		$Obj = new TemplateLoader( );
		$Obj->Module = 'SocioRating';
		$Obj->Template = 'templates/someoneratedyou.tpl';
		$Obj->Name = $RatedAcc->fullname;
		$Obj->acc = $RatedAcc->login;
		$Obj->FullName = $RatedAcc->fullname;
		$Obj->Rater = $RaterAcc->fullname;
		$Obj->Accurate = $Accurate;
		$Obj->Friendly = $Friendly;
		$Obj->Helpful = $Helpful;
		$Obj->Gooder = $Gooder;
		$Obj->Critical = $Critical;
		$Obj->Trustworthy = $Trustworthy;
		$Obj->Creative = $Creative;
		$Obj->Comment = $Comment;
		
		$to = $RatedAcc->fullname . ' <' . $RatedAcc->email . '>';
		$title = $RaterAcc->fullname . ' heeft jou beoordeeld!';
		$mail = $Obj->GetText( );
		
		Mail::Send($to, $title, $mail);
	}
}